
import duckdb

# 连接数据库
DB_HOST = "1.15.241.228"
DB_USERNAME = "root"
DB_PASSWORD = "qiuwen123456"
DB_PORT = 3306
DB_NAME = "data"
PARAMETERS = 'host={DB_HOST} user={DB_USERNAME} password={DB_PASSWORD} port={DB_PORT} database={DB_NAME}'.format(
    DB_HOST=DB_HOST,
    DB_USERNAME = DB_USERNAME,
    DB_PASSWORD = DB_PASSWORD,
    DB_PORT = DB_PORT,
    DB_NAME = DB_NAME
                                                                                                   )
ATTACH_QUERY = "ATTACH '{PARAMETERS}' AS mysql_db (TYPE MYSQL)".format(PARAMETERS = PARAMETERS)
USE_QUERY = "USE mysql_db;"

# 增删改查

with duckdb.connect() as conn:
    conn.install_extension("mysql")
    conn.load_extension("mysql")
    conn.sql(ATTACH_QUERY)
    conn.sql(USE_QUERY)
    conn.sql("SHOW TABLES").show()
    # for i in range(1,100):
    #   conn.sql("INSERT INTO tbl VALUES ({i}, 'DuckDB42');".format(i=i))
